<template>
  <div id="userLoginPage">
    <h1 class="title">胡图图库 | 用户注册</h1>
    <div class="desc">——智能协同云图库平台</div>
    <a-form
      :model="formState"
      name="basic"
      autocomplete="off"
      @finish="handleSumit"
    >
      <a-form-item name="userAccount" :rules="[{ required: true, message: '请输入账号！' }]">
        <a-input v-model:value="formState.userAccount" placeholder="请输入账号"/>
      </a-form-item>

      <a-form-item
        name="userPassword"
        :rules="[{ required: true, message: '请输入密码!' }, {min: 8, message: '密码不能小于8位'}]"
      >
        <a-input-password v-model:value="formState.userPassword" placeholder="请输入密码"/>
      </a-form-item>
      <a-form-item
        name="checkPassword"
        :rules="[{ required: true, message: '请确认密码是否正确!' }, {min: 8, message: '密码不能小于8位'}]"
      >
        <a-input-password v-model:value="formState.checkPassword" placeholder="请再次输入密码"/>
      </a-form-item>
      <div class="tips">
        已有账号？
        <router-link to="/user/login">去登录</router-link>
      </div>
      <a-form-item >
        <a-button type="primary" html-type="submit" style="width: 100%">注册</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>
<script lang="ts" setup>
import { reactive } from 'vue'
import {useRouter} from "vue-router";
import {useLoginUserStore} from "@/stores/useLoginUserStore";
import {message} from "ant-design-vue";
import {userRegisterUsingPost} from "@/api/userController";

const router = useRouter();
const loginUserStore = useLoginUserStore();

const formState = reactive<API.UserRegisterRequest>({
  userAccount: '',
  userPassword: '',
  checkPassword: '',
})

/**
 * 提交表单
 * @param values
 */
const handleSumit = async (values: any) => {
  // 判断两次输入密码是否一致
  if(formState.userPassword !== formState.checkPassword) {
    message.error('两次输入的密码不一致');
    return
  }
  const res = await userRegisterUsingPost(values);
  // 注册成功，跳转到登录页
  if(res.data.code === 0 && res.data.data) {
    message.success('注册成功，跳转到登录页~')
    router.push({
      path: '/user/login',
      replace: true,
    })
  }else {
    message.error('注册失败，' + res.data.message);
  }

}

</script>
<style scoped>
#userLoginPage {
  max-width: 360px;
  margin: 0 auto;
}
.title {
  text-align: center;
  margin-bottom: 16px;
}
.desc {
  text-align: right;
  margin-bottom: 13px;
}
.tips {
  margin-bottom: 16px;
  font-size: 13px;
  color: #888;
  text-align: right;
}
</style>
